<template>
  <div>
    <el-row>
      <el-page-header @back="goBack"></el-page-header>
    </el-row>
    <el-row style="margin-top: 30px;">
      <el-form ref="form" label-position="left" :model="noticeForm" label-width="80px">
        <el-form-item label="通知名称">
          <el-col :span="6">
            <el-input style="width: 350px" v-model="noticeForm.noticeTitle"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="持续时间">
          <el-col :span="6">
            <el-date-picker
              v-model="noticeForm.date"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              start-placeholder="通知开始日期"
              end-placeholder="通知结束日期">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="通知内容">
          <el-col :span="18">
            <quill-editor
              class="editor"
              ref="myQuillEditor"
              v-model="noticeForm.noticeContent"
              :options="editorOption"
            />
          </el-col>
        </el-form-item>
        <el-form-item size="large" style="margin-top: 60px">
          <el-col :span="2">
            <el-button v-if="noticeId===undefined" type="primary" @click="createNotice">创建通知</el-button>
            <el-button v-else type="primary" @click="saveNotice">保存通知</el-button>
          </el-col>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'CourseNotice',
  components: {
    quillEditor
  },
  data () {
    // 工具栏配置
    const toolbarOptions = [
      ['bold', 'italic', 'underline', 'strike'],       // 加粗 斜体 下划线 删除线
      ['blockquote', 'code-block'],                    // 引用  代码块
      [{ list: 'ordered' }, { list: 'bullet' }],       // 有序、无序列表
      [{ indent: '-1' }, { indent: '+1' }],            // 缩进
      [{ size: ['small', false, 'large', 'huge'] }],   // 字体大小
      [{ header: [1, 2, 3, 4, 5, 6, false] }],         // 标题
      [{ color: [] }, { background: [] }],             // 字体颜色、字体背景颜色
      ['clean'],                                       // 清除文本格式
    ]
    return {
      editorOption: {
        //  富文本编辑器配置
        modules: {
          //工具栏定义的
          toolbar: toolbarOptions
        },
        //主题
        theme: 'snow',
        placeholder: '请输入正文'
      },
      courseId: undefined,
      noticeId: undefined,
      noticeForm: {
        noticeTitle: '',
        date: [],
        noticeContent: '',
      },
    }
  },
  methods: {
    goBack () {
      this.$router.go(-1)
    },
    createNotice () {
      this.$http.post('/api/notice', {
        courseId: this.courseId,
        noticeTitle: this.noticeForm.noticeTitle,
        noticeStartTime: this.noticeForm.date[0],
        noticeEndTime: this.noticeForm.date[1],
        noticeContent: this.noticeForm.noticeContent,
      }).then(res => {
        if (res.data.code === 2000) {
          this.$message({
            type: 'success',
            message: '创建通知成功!',
            duration: 1500,
          })
          this.$router.push('/course/notice')
        } else {
          this.$message({
            type: 'error',
            message: '创建通知失败!',
            duration: 1500,
          })
        }
      })
    },
    saveNotice () {
      this.$http.post('/api/notice/edit', {
        noticeId: this.noticeId,
        courseId: this.courseId,
        noticeTitle: this.noticeForm.noticeTitle,
        noticeStartTime: this.noticeForm.date[0],
        noticeEndTime: this.noticeForm.date[1],
        noticeContent: this.noticeForm.noticeContent,
      }).then(res => {
        if (res.data.code === 2000) {
          this.$message({
            type: 'success',
            message: '修改通知成功!',
            duration: 1500,
          })
          this.$router.push('/course/notice')
        } else {
          this.$message({
            type: 'error',
            message: '修改通知失败!',
            duration: 1500,
          })
        }
      })
    },
    init () {
      this.courseId = this.$store.state.course.courseId
      this.noticeId = this.$route.params.noticeId
      if (this.noticeId !== undefined) {
        //表示是编辑
        this.$http.get('/api/notice/' + this.noticeId).then(res => {
          this.noticeForm.noticeTitle = res.data.result.noticeTitle
          this.noticeForm.date = [new Date(res.data.result.noticeStartTime), new Date(res.data.result.noticeEndTime)]
          this.noticeForm.noticeContent = res.data.result.noticeContent
        })
      }
    },
  },
  created () {
    this.init()
  },
}
</script>

<style>
.editor {
  line-height: normal !important;
  height: 200px;
}

.ql-snow .ql-tooltip[data-mode="link"]::before {
  content: "请输入链接地址:";
}

.ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  border-right: 0px;
  content: "保存";
  padding-right: 0px;
}

.ql-snow .ql-tooltip[data-mode="video"]::before {
  content: "请输入视频地址:";
}

.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
  content: "14px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  content: "10px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  content: "18px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  content: "32px";
}

.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
  content: "文本";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  content: "标题1";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  content: "标题2";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  content: "标题3";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  content: "标题4";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  content: "标题5";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  content: "标题6";
}

.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: "标准字体";
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  content: "衬线字体";
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  content: "等宽字体";
}
</style>

